<?php

	include('./../includes/db_connect.php');
	include_once('./../classes/pagination.class.php');

        //pagination

        //main pagination settings
	$items = 8;
	$page = 1;

        if(isset($_GET['page']) and is_numeric($_GET['page']) and $page = $_GET['page']){

		$limit = " LIMIT ".(($page-1)*$items).",$items";             
        }else{

		$limit = " LIMIT $items";
        }



/*
	$query = "SELECT u.id, u.user_name, l.location, u.last_login, u.status 
                  FROM user u, lst_location l 
                  WHERE l.id = u.location_id";


	$result  = mysql_query($query);
        $records = mysql_num_rows($result);
*/

              $sqlStr = "SELECT u.id, 
                                u.user_name, 
                                l.location, 
                                u.last_login, 
                                u.status 
                         FROM user u, lst_location l 
                         WHERE l.id = u.location_id";

	      $sqlStrAux = "SELECT count(*) as total FROM user";


	      $aux = Mysql_Fetch_Assoc(mysql_query($sqlStrAux));



	      $query = mysql_query($sqlStr.$limit);


         echo "<span id=\"admin_records_indicator\">".$aux['total']."</span> 
              <h2>Users were found in the database.</h2>";


	      if($aux['total']>0){
			$p = new pagination;
			$p->Items($aux['total']);
			$p->limit($items);
			$p->target("admin.php");
			$p->currentPage($page);
			$p->calculate();
			$p->changeClass("pagination");
?>

                    <table class="table1">
                        <thead>
  	                   <tr>
    	                      <th>User</th>
                              <th>Location</th>
                              <th>Last Login</th>
                              <th>Logins This Month</th>
                              <th>Enabled</th>
                           </tr>
                        </thead>
                        <tbody>

<?php
		
                    $i=0;
		
                    while($row = mysql_fetch_array($query)) {

			$thisMonthsql = date("Y-m-d", mktime(0,0,0,date("m"), 1, date("Y")));
			$nextMonthsql = date("Y-m-d", mktime(0,0,0,date("m")+1, 1, date("Y")));

                        //echo date("n");echo"<br />";



                       //mine
                       $thisMonth = date("n");
                                           

 


			$countQuery = "SELECT count(id)
                                       AS count 
                                       FROM user_history 
                                       WHERE action_id = 1 
                                       AND user_id = ".$row['id'];


			$countResult = mysql_query($countQuery);


			$countRow = mysql_fetch_array($countResult);
			if($i%2!=0)
				echo "<tr class='odd'>";
			else
				echo "<tr>";
			echo "<td><a href=\"./admin.php?Action=viewUser&id=".$row['id']."\">".$row['user_name']."</a></td>";
			echo "<td>".$row['location']."</td>";
			echo "<td>".$row['last_login']."</td>";
			echo "<td>".$countRow['count']."</td>";
			if($row["status"] == 0) {
				echo "<td><img src=\"./img/cancel.png\"/></td>";
			} else {
				echo "<td><img src=\"./img/check.png\"/></td>";
			}
			echo "</tr>";
			$i++;
		}
?>
                </tbody>
                </table>

<?php
                $p->show();

         }else{
		echo "There no are records to paginate.";
         } //ENDS MAIN if
?>